package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.consts.SystemConst;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.model.SysRole;
import com.ccp.dev.system.util.SysRoleUtil;
import com.ccp.dev.workflow.model.ProcessRun;
import com.ccp.dev.workflow.model.TaskOpinion;
import com.ccp.dev.workflow.model.bpm.ProcessTask;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassDescribe:流程相关的一些方法，供其他项目使用  Service类
 * @author :jiayueqiao
 * Date: 2019-03-24
 * Since:1
 */
@Service
public class CommonFlowService {

    @Resource
    private TaskOpinionService taskOpinionService;
    @Resource
    private BpmService bpmService;

    @Resource
    private ProcessRunService processRunService;

    /**
     * 根据流程运行id查询审批历史节点及状态
     * @param runId 流程运行id
     * @return 审批意见列表
     */
    public List<TaskOpinion> getOpinionList(String runId){
        //取得关联的流程实例ID
        List<TaskOpinion> list=taskOpinionService.getByRunId(runId);
        //设置执行人
        list = taskOpinionService.setTaskOpinionExecutor(list);
        return list;
    }

    /**
     * 我的请求列表
     * @param request http请求
     * @return 流程运行id和审批历史的关联关系
     */
    public Map<String, TaskOpinion> myRequestList(HttpServletRequest request) {
        // 构造参数集合，此处无需前台传入其他参数
        QueryFilter filter = new QueryFilter(request, false);
        filter.addFilter("creatorId", ContextUtil.getCurrentUserId());
        // 查询请求列表
        List<ProcessRun> processRunList = processRunService.queryMyRequestList(filter);
        Map<String, TaskOpinion> opinionMap = new HashMap<>(5);
        // 在流程与审批对象之间建立映射
        for (ProcessRun processRun : processRunList) {
            List<TaskOpinion> opinionList = taskOpinionService.getByProcInstId(processRun.getProcInstId(), false);
            TaskOpinion opinion = taskOpinionService.setTaskOpinionExecutor(opinionList).get(0);
            opinionList.get(0).setProcessName(processRun.getProcessName());
            opinionMap.put(processRun.getRunId(), opinion);
        }
        return opinionMap;
    }

    /**
     * 待办任务列表
     * @param request 请求
     * @return 任务列表
     */
    public List<ProcessTask> pendingMattersList(HttpServletRequest request) {
        // 构造参数集合，此处无需前台传入其他参数
        QueryFilter queryFilter = new QueryFilter(request, true);
        if(StringUtil.isNotEmpty(ContextUtil.getCurrentUserId()) && !SystemConst.SUPER_ADMIN_ID.equals(ContextUtil.getCurrentUserId())){
            SysRole currentSysRole = SysRoleUtil.getCurrentRole(request);
            if(null!=currentSysRole){
                queryFilter.addFilter("switchRoleId", currentSysRole.getRoleId());
            }
        }
        // 查询待办任务
        List<ProcessTask> list = bpmService.queryMyTask(queryFilter);
        List<ProcessTask> pendingMatters = new ArrayList<>();
        // 去掉驳回任务
        for(ProcessTask task : list) {
            if ("6".equals(task.getTaskStatus())) {
                continue;
            }
            pendingMatters.add(task);
        }
        return pendingMatters;
    }

    /**
     * 获取驳回列表
     * @param request http请求
     * @return 当前任务与前一个审批意见的对应关系
     */
    public Map<String, TaskOpinion> rejectTaskList(HttpServletRequest request) {
        // 构造参数集合，此处无需前台传入其他参数
        QueryFilter filter = new QueryFilter(request, false);
        // 驳回状态
        filter.addFilter("taskStatus", 6);
        // 查询驳回任务列表
        List<ProcessTask> taskList = bpmService.queryMyTask(filter);
        Map<String, TaskOpinion> opinionMap = new HashMap<>(5);
        // 将任务与审批意见对象建立映射
        for (ProcessTask task : taskList) {
            TaskOpinion taskOpinion = taskOpinionService.getByProcInstId(task.getProcessInstanceId(), false).get(1);
            opinionMap.put(task.getId(), taskOpinion);
        }

        return opinionMap;
    }

    /**
     * 查询当前登陆人处理过的流程任务数据，包括已经结束的流程
     * @return List<ProcessRun>
     */
    public List<ProcessRun> queryMyProcessedMattersList(){
        String assignee = ContextUtil.getCurrentUserId();
        return processRunService.queryMyProcessedMattersList(assignee);
    }
}
